package cluster_node.scheduler.type_config;

import cluster_node.models.ReadRequestWrapper;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.request.ReadRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TypeDeviceUploadHelper {
    private Map<Integer, List<Integer>> responsibleMap;
    private List<ReadRequestWrapper> requests;

    public TypeDeviceUploadHelper(Map<Integer, List<Integer>> responsibleMap) {
        this.responsibleMap = responsibleMap;
        clearAndGenerateRequests();
    }

    public Map<Integer, List<Integer>> getResponsibleMap() {
        return responsibleMap;
    }

    public void setResponsibleMap(Map<Integer, List<Integer>> responsibleMap) {
        this.responsibleMap = responsibleMap;
        clearAndGenerateRequests();
    }

    public List<ReadRequestWrapper> getRequests() {
        return requests;
    }

    private void clearAndGenerateRequests() {
        requests = new ArrayList<>();

        for (Map.Entry<Integer, List<Integer>> entry : responsibleMap.entrySet()) {
            Integer objectId = entry.getKey();
            List<Integer> resources = entry.getValue();

            for (int resource : resources) {
                requests.add(new ReadRequestWrapper(
                        new ReadRequest(ContentFormat.JSON, objectId, 0, resource),
                        objectId,
                        resource));
            }
        }
    }
}